home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.12 Dec 89 / ASM Program / ScalarMult.a < prev   
Encoding:
Text File  |  1988-07-01  |  5.6 KB  |  244 lines  |  [TEXT/MPS ]

  1.     MC68881                        ; We will be using
  2.                                 ; instructions for
  3.                                 ; the 68881 chip
  4.     MACHINE MC68020                ; optimize for the
  5.                                 ; 68020, if you don't
  6.                                 ; have one remove
  7.                                 ; this line
  8.     
  9. ScaleMult     FUNC     EXPORT
  10.                                 ; this lets the rest
  11.                                 ; of the world know
  12.                                 ; about our function
  13.  
  14. _Debugger  OPWORD $A9FF            ; this will pop us
  15.                                 ; into Macs bugs so
  16.                                 ; we can follow every
  17.                                 ; instruction and
  18.                                 ; observe the 
  19.                                 ; registers
  20.  
  21.  
  22. ; The following are all displacements so we
  23. ; can address our data on the stack, relative
  24. ; to the address stored in A6
  25.  
  26. result    EQU    20            ; two bytes for our
  27.                         ; integer result
  28. Scalar    EQU    16            ; 4 bytes (address of
  29.                         ; the scalar)
  30. inMat    EQU    12            ; 4 bytes (address of
  31.                         ; the input Matrix)
  32. outMat    EQU    8
  33.                         ; 4 bytes (address of
  34.                         ; the output Matrix)
  35. ReturnAdd     EQU    4        ; 4 bytes
  36. superScalar EQU    -12
  37.                         ; 12 bytes (hold a
  38.                         ; place for our 68881
  39.                         ; version of the
  40.                         ; scalar)
  41. oldA2    EQU     -14        ; old the old value
  42.                         ; of A2
  43. oldA3    EQU    -16            ; old the old value
  44.                         ;of A3
  45.  
  46.  
  47. ; *** Add the following features (to make
  48. ; this a “real” program):
  49. ;         1) make sure the number of 
  50. ;        rows and columns in the input 
  51. ;        Matrix jives with that of the 
  52. ;        the output matrix.
  53. ;        2) Fix the error message to 
  54. ;        tell the Pascal Program if 
  55. ;        there was a problem, such as 
  56. ;        if: rows X columns ≠ vector 
  57. ;        size
  58.  
  59. ; ***** The first “real” line of our program ;         (it’s about time!)
  60.  
  61.     _Debugger            ; jump into Macsbugs
  62.                         ; so we can follow
  63.                         ; what's going on
  64.  
  65. ; move all the initial parameters off the 
  66. ; stack from the calling routine "function 
  67. ; ScaleMult (scalar:extended;var inMatrix,
  68. ; outMatrix:matrix) :error"
  69.             
  70.     link    A6,#-12    ; push A6 (the frame
  71.             ; pointer) onto the
  72.             ; stack , load SP
  73.             ; into A6, then
  74.             ; subtract off 12
  75.             ; bytes from A7 to
  76.             ; make room for our
  77.             ; variables
  78.  
  79. ; *******************************************
  80. ; after the link instruction
  81. ; the stack looks as follows:
  82. ;
  83. ; high
  84. ;        integer (2 bytes)
  85. ;        ------------    <- 20(A6)
  86. ;        scalar (4 bytes)
  87. ;        ------------    <- 16(A6)
  88. ;        inMatrix (4 bytes)
  89. ;        ------------    <- 12(A6)
  90. ;        oldMatrix (4 bytes)
  91. ;        ------------    <-  8(A6)
  92. ;        returnAddress (4 bytes)
  93. ;        ------------    <-  4(A6)
  94. ;        old A6 (4 bytes)
  95. ;        ------------    <-  (A6)
  96. ;         superscalar (12 bytes)
  97. ;        ------------    <- -12(A6) 
  98. ;                 and initial (A7)
  99. ;    low        
  100. ;
  101. ; *******************************************
  102.  
  103.  
  104.             
  105. ; now move the addresses onto the chip so we ; can work with them
  106.  
  107.     MOVE.L    A2,-(A7)            ; push A2 onto the
  108.                                 ; stack to save it
  109.     MOVE.L    A3,-(A7)            ; do the same to A3
  110.     MOVE.L    inMat(A6),A2
  111.                                 ; get the pointer to
  112.                                 ; the address of
  113.                                 ; inMatrix
  114.     MOVE.L    4(A2),A0            ; get the address of
  115.                                 ; the input matrix
  116.                                 ; (4 bytes from the
  117.                                 ; start of our matrix
  118.                                 ; data structure )
  119.     MOVE.L    outMat(A6),A2    
  120.     MOVE.L    4(A2),A1            ; get the address of
  121.                                 ; the output matrix
  122.     MOVE.L    inMat(A6),A2        ; get the number of
  123.                                 ; rows (first byte of
  124.                                 ; Matrix record)
  125.     MOVE.W    (A2),D0
  126.     MOVE.W    2(A2),D1            ; then get the number
  127.                                 ; of columns (3rd 
  128.                                 ; byte of matrix
  129.                                 ; record
  130.  
  131.     MOVE.L    scalar(A6),A2
  132.                                 ; we will now move
  133.                                 ; the scalar to a
  134.                                 ; place with a bit
  135.                                 ; more room
  136.     LEA    superScalar(A6),A3
  137.     CLR.W    (A3)+
  138.     MOVE.L    (A2)+,(A3)+         ; move the first 4
  139.                                 ; bytes
  140.  
  141.     MOVE.L    (A2)+,(A3)+            ; move the next 4
  142.                                 ; bytes
  143.  
  144.     MOVE.W    (A2),(A3)            ; move the last byte
  145.             
  146.     LEA    superScalar(A6),A2
  147.     MOVE.L    (A2),D2                ; get the top
  148.                                 ; long-word of
  149.                                 ; the scalar, shift
  150.                                 ; it, then put it
  151.                                 ; back
  152.     LSL.L    #8,D2
  153.     LSL.L    #8,D2
  154.     MOVE.L    D2,(A2)
  155.  
  156.     FMOVE.X    (A2),FP1            ; get the modified
  157.                                 ; scalar and store it
  158.                                 ; on the 68881
  159.             
  160. Loop    MOVE.L    (A0),D2            ; get the current
  161.                                 ; high Long word of
  162.                                 ; the element from
  163.                                 ; the input matrix
  164.     LSL.L    #8,D2
  165.     LSL.L    #8,D2
  166.     MOVE.L    D2,(A0)
  167.             
  168.     FMOVE.X    (A0),FP0            ; get the current
  169.                                 ; element
  170.     FMUL.X    FP1,FP0                ; multiply by the
  171.                                 ; scalar
  172.     FMOVE.X    FP0,(A1)            ; put the element
  173.                                 ; back in RAM
  174.     MOVE.L    (A1),D2                ; now shift the high
  175.                                 ; Long word back (in
  176.                                 ; the output
  177.                                 ; matrix,and then put
  178.                                 ; it away in RAM
  179.     LSR.L    #8,D2
  180.     LSR.L    #8,D2
  181.     MOVE.L    D2,(A1)
  182.     MOVE.L    (A0),D2                ; now shift the high
  183.                                 ; Long word back (in
  184.                                 ; the input matrix)
  185.                                 ; and then put it
  186.                                 ; away
  187.     LSR.L    #8,D2
  188.     LSR.L    #8,D2
  189.     MOVE.L    D2,(A0)
  190.  
  191.     ADD    #12,A0                    ; increment the
  192.                                 ; element's address
  193.                                 ; (to get the next
  194.                                 ; element)
  195.     ADD    #12,A1
  196.             
  197.     DBLT    D1,Loop                ; decrement the
  198.                                 ; number of columns
  199.                                 ; and test
  200. ; if we are here we have gone through one 
  201. ; column
  202.     MOVE.L    inMat(A6),A2
  203.     MOVE.W    2(A2),D1            ; restore the number
  204.                                 ; of columns
  205.     DBLT    D0,Loop                ; test if we have
  206.                                 ; completed through
  207.                                 ; all the rows
  208. ; if we are here, we have gone through all 
  209. ; the rows
  210.  
  211.  
  212. ; since we have done our work let’s put away 
  213. ; our toys (clear the stack of all the 
  214. ; garbage) and go home.
  215.  
  216.     MOVE.L    (A7)+,A3            ; pop off the old A3
  217.     MOVE.L    (A7)+,A2            ; pop off the old A2
  218.     UNLK A6            
  219.     MOVE.L    (A7)+,A0            ; save the return address
  220.     ADD.L    #12,A7    ; move the stack
  221.                     ; pointer clear all
  222.                     ; the data off the
  223.                     ; stack
  224.             
  225.     CLR.W    (A7)    ; replace the return
  226.                     ; value on the stack
  227.                     ; with ours
  228.  
  229. ; ** NOTE: we are pushing a value of zero 
  230. ; onto the stack, meaning that nothing went 
  231. ; wrong.  This is bogus and in the real 
  232. ; version we need to fix this!
  233.  
  234.  
  235.     MOVE.L    A0,-(A7)            ; push the return
  236.                                 ; address back onto
  237.                                 ; the stack
  238.                             
  239.     RTS                            ; return to reality
  240.                                 ; (our calling
  241.                                 ; program)
  242.     ENDF
  243.     END
  244.